class code6 {
    // 岛屿数量
    int count;
    int m;
    int n;
    public int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j] == '1') {
                    dfs(grid,i,j);
                    count++;
                }
            }
        }

        return count;
    }

    public void dfs(char[][] grid,int i,int j) {
        //判断越界
        if(i<0||i>=m||j<0||j>=n) {
            return;
        }

        if(grid[i][j] == '1') {
            grid[i][j] = '0';
        } else {
            return;
        }

        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);

    }

}